from sqlalchemy import Column, Integer, String, DateTime, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from ..core.database import Base


class Notebook(Base):
    """笔记本/文件夹模型"""
    __tablename__ = "notebooks"
    
    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey('users.id'), nullable=False, index=True)
    name = Column(String(100), nullable=False)
    parent_id = Column(Integer, ForeignKey('notebooks.id'), nullable=True, index=True)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    
    # 关联关系
    user = relationship("User", back_populates="notebooks")
    notes = relationship("Note", back_populates="notebook")
    
    # 自关联：父子笔记本
    children = relationship("Notebook", backref="parent", remote_side=[id])

